package com.example.cd5160866.materialdesgin.capture;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.cd5160866.materialdesgin.R;

import utils.utils.CaputrueUtils;

/**
 * Created by cd5160866 on 2018/1/18.
 */

public class CaptureDialog extends Dialog implements View.OnClickListener {
    private FrameLayout rootView;
    private int showColor = 0xCC000000;

    private SparseArray<ImageView> captureImageView = new SparseArray<>();


    public CaptureDialog(@NonNull Context context) {
        super(context, R.style.DialogCaputre);
        init(context);
    }

    private void init(Context context) {
        rootView = new FrameLayout(context);
        rootView.setOnClickListener(this);
        setShadowColor(showColor);
        setContentView(rootView);
    }

    public void setShadowColor(int color) {
        this.showColor = color;
        rootView.setBackgroundColor(showColor);
    }

    public void setShadowClickListener(View.OnClickListener onClickListener) {
        rootView.setOnClickListener(onClickListener);
    }

    public void clear() {
        rootView.removeAllViews();
        captureImageView.clear();
    }

    /**
     * 获取你高亮展示的那个view 生成的ImageView 如果你需要拿到这个ImageView的话
     *
     * @param targetView 高亮的view
     * @return
     */
    public ImageView getCaputureImageView(@NonNull View targetView) {
        return captureImageView.get(targetView.getId(), null);
    }

    public CaptureDialog addCaptureView(@NonNull View targetView) {
        return addCaptureView(targetView, CaptureViewDecorate.getDefault());
    }

    /**
     * 将目标view 高亮
     *
     * @param targetView          你想要高亮的view
     * @param captureViewDecorate 这个类可以修饰你的高亮view 你可以为他添加背景 额外添加其他eiw 附着在它周围
     */
    public CaptureDialog addCaptureView(@NonNull View targetView, @NonNull final CaptureViewDecorate captureViewDecorate) {
        // init ImageView for capture
        ImageView ivCapture = new ImageView(getContext());
        ivCapture.setImageBitmap(CaputrueUtils.getCaptureView(targetView));
        int paddingHorizontal = captureViewDecorate.getCaptureEdgeHorizontal();
        int paddingVertical = captureViewDecorate.getCaptureEdgeVertical();
        ivCapture.setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical);
        if (null != captureViewDecorate.getCaptureBackGround())
            ivCapture.setBackgroundDrawable(captureViewDecorate.getCaptureBackGround());
        // the imageView with that targetView already  attached
        if (null != captureImageView.get(targetView.getId(), null))
            return this;
        captureImageView.append(targetView.getId(), ivCapture);


        // set image location
        int[] ints = new int[2];
        targetView.getLocationOnScreen(ints);
        ViewGroup.MarginLayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int imageMarginX = ints[0] - captureViewDecorate.getCaptureEdgeHorizontal();
        int imageMarginY = ints[1] - captureViewDecorate.getCaptureEdgeVertical() - CaputrueUtils.getStatusBarHeight(getContext());
        params.setMargins(imageMarginX, imageMarginY, 0, 0);
        rootView.addView(ivCapture, params);

        // set decorateView for image
        if (null == captureViewDecorate.getDecorateView())
            return this;
        ViewGroup.MarginLayoutParams paramsDecorateView = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsDecorateView.setMargins(imageMarginX + captureViewDecorate.getDecorateViewOffSetX(), imageMarginY + captureViewDecorate.getDecorateViewOffSetY(), 0, 0);
        rootView.addView(captureViewDecorate.getDecorateView(), paramsDecorateView);
        if (null == captureViewDecorate.getListener())
            return this;
        captureViewDecorate.getDecorateView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                captureViewDecorate.getListener().decorateViewClick(CaptureDialog.this);
            }
        });
        return this;
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    public CaptureDialog showCapture() {
        show();
        return this;
    }

    public static class Builder {
        CaptureParams P;

        public Builder(Context context) {
            P = new CaptureParams(context);
        }

        public Builder setShadowColor(int color) {
            P.shadowColor = color;
            return this;
        }

        public Builder setOnShadowClickListener(View.OnClickListener onShadowClickListener) {
            P.onShadowClickListener = onShadowClickListener;
            return this;
        }

        public Builder setCancelableBackPressed(boolean cancelable) {
            P.cancelableBackPressed = cancelable;
            return this;
        }

        public Builder setOnDismissListener(OnDismissListener listener) {
            P.onDismissListener = listener;
            return this;
        }

        public CaptureDialog create() {
            CaptureDialog dialog = new CaptureDialog(P.context);
            dialog.setShadowColor(P.shadowColor);
            dialog.setOnDismissListener(P.onDismissListener);
            dialog.setCancelable(P.cancelableBackPressed);
            if (null != P.onShadowClickListener)
                dialog.setShadowClickListener(P.onShadowClickListener);
            return dialog;
        }

    }

    private static class CaptureParams {
        public CaptureParams(Context context) {
            this.context = context;
        }

        Context context;
        boolean cancelableBackPressed = true;
        OnDismissListener onDismissListener;
        int shadowColor = 0xCC000000;
        View.OnClickListener onShadowClickListener;
    }
}

